package com.lijinquan.tank;

import com.lijinquan.tank.abstractfactory.*;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijinquan on 2020/3/16.
 */
public class TankFrame extends Frame {


    Tank tank = new Tank(200, 200, Dir.DOWN,Group.GOOD, this);
    public java.util.List<BaseBullet> bullets = new ArrayList<>();
    List<Tank> tanks=new ArrayList<>();

    public List<BaseExplode> explodes=new ArrayList<>();

    public static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960;
    public GameFactory gameFactory=new RectFactory();

    public TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setVisible(true);
        setResizable(false);
        setTitle("tank_war");

        this.addKeyListener(new MyKeyListener());

        addWindowListener(new WindowAdapter() {//添加window监听器
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.black);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }


    @Override
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹数量:" + bullets.size(), 10, 60);
        g.drawString("坦克数量:" + tanks.size(), 10, 80);
        g.setColor(c);

        tank.paint(g);
        for (int i=0;i<bullets.size();i++) {
            bullets.get(i).paint(g);
        }
        for(int j=0;j<tanks.size();j++){
            tanks.get(j).paint(g);
        }

        for (int i=0;i<bullets.size();i++) {
            for(int j=0;j<tanks.size();j++)
                bullets.get(i).collideWith(tanks.get(j));
        }

        for(int y=0;y<explodes.size();y++){
            explodes.get(y).paint(g);
        }


    }


    class MyKeyListener extends KeyAdapter {


        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
            }

            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    tank.fire();
                    break;
            }
            setMainTankDir();
        }

        public void setMainTankDir() {


            if (!bL && !bR && !bD && !bU) {
                tank.setMoving(false);
            } else {
                tank.setMoving(true);

                if (bL) tank.setDir(Dir.LEFT);
                if (bU) tank.setDir(Dir.UP);
                if (bR) tank.setDir(Dir.RIGHT);
                if (bD) tank.setDir(Dir.DOWN);

            }
        }
    }
}
